Skip to content

feat(nextjs): Add signature verification of token passed from middleware - #3121

Merged
brkalow merged 7 commits into
mainfrom
brk.feat/next-token-signature
Apr 10, 2024
Merged

feat(nextjs): Add signature verification of token passed from middleware#3121
brkalow merged 7 commits into
mainfrom
brk.feat/next-token-signature

Conversation

@brkalow

@brkalow brkalow commented Apr 4, 2024

Copy link
Copy Markdown
Member

Description

Introduce signature verification of the JWT that is passed from middleware to the Next.js origin. We're only introducing this functionality into the Next.js SDK as it's the only framework (currently) that involves a network hop between verification of the token and usage.

The goal here was to retain the sync API of auth(), and so we're not using the native async crypto modules at this time. Instead, we're using crypto-js, which exposes sync crpyto APIs.

To generate a signature, we're passing the verified JWT and the app's clerk secret key to generate an HMAC with SHA1. The performance impact of this is negligible, with a <1ms measured impact locally. SHA1 is acceptable for use here, as the token has already been verified and we aren't concerned with a collision. We're validating that the token we verified in the middleware is the same one we're using at the app's origin.

This also further mitigates the IDOR vulnerability from earlier this year, ensuring we are always using the previously verified token.

Checklist

  • npm test runs as expected.
  • npm run build runs as expected.
  • (If applicable) JSDoc comments have been added or updated for any package exports
  • (If applicable) Documentation has been updated

Type of change

  • 🐛 Bug fix
  • 🌟 New feature
  • 🔨 Breaking change
  • 📖 Refactoring / dependency upgrade / documentation
  • other:

@changeset-bot

changeset-bot Bot commented Apr 4, 2024

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 261639b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 7 packages
Name Type
@clerk/backend Minor
@clerk/nextjs Minor
@clerk/express Patch
@clerk/fastify Patch
gatsby-plugin-clerk Patch
@clerk/remix Patch
@clerk/clerk-sdk-node Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@panteliselef

Copy link
Copy Markdown
Contributor

@brkalow is this something that only concerns nextjs ? Can't we move this to backend ?

@brkalow

brkalow commented Apr 5, 2024

Copy link
Copy Markdown
Member Author

@panteliselef Yeah it's only a Next.js concern, due to the handoff from middleware -> app backend.

@brkalow
brkalow marked this pull request as ready for review April 10, 2024 03:20
@brkalow
brkalow requested a review from nikosdouvlis April 10, 2024 03:20

@nikosdouvlis nikosdouvlis left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏🏻

Comment thread packages/nextjs/package.json Outdated
Comment thread packages/nextjs/package.json Outdated
@brkalow
brkalow enabled auto-merge (squash) April 10, 2024 16:36
@brkalow
brkalow merged commit ecb60da into main Apr 10, 2024
@brkalow
brkalow deleted the brk.feat/next-token-signature branch April 10, 2024 16:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants